﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mota.XMLControls;

namespace Mota.EditModule
{
    public static class GuiControlExtensions
    {
        public static void Rfsh(this GuiControl control,Dictionary<string,GuiControl> mainControls,
                                            BindingSource mainBs,
                                            ConnectionConfigs configs)
        {
            if ((control is IDatasourceObject) &&
              control.XmlAttributes.ContainsKey("XMLAttribute") &&
                control.XmlAttributes.ContainsKey("_KeyField") &&
                control.XmlAttributes.ContainsKey("_Fields") &&
                control.XmlAttributes.ContainsKey("NameOrig"))
            {
                String sourceViewName = control.XmlAttributes["NameOrig"];

                BindingSource bs = new BindingSource();


                DataTable dt;
                List<IStatement> statements = new List<IStatement>();
                if ((control as IDatasourceObject).UseOnlyActive)
                    statements.Add(new EqualStatement("IsActive", "1"));

                if (mainControls.Count > 0)
                {
                    foreach (var item in mainControls)
                    {
                        statements.Add(new EqualStatement(item.Key, item.Value.GetValue()));
                    }
                }
                // TODO: keyfields intersect fields
                if (statements.Count > 0)
                    dt = Model.GetDataTable(configs,
                                                sourceViewName,
                                                statements.Aggregate((u1, u2) => new AndStatement(u1, u2)),
                                                (control as IDatasourceObject).UseGroupBy,
                                                control.XmlAttributes["_Fields"]);
                else
                    dt = Model.GetDataTable(configs,
                                                sourceViewName,
                                                (control as IDatasourceObject).UseGroupBy,
                                                control.XmlAttributes["_Fields"]);



                if ((dt.Rows.Count > 1) && (control.XmlAttributes.ContainsKey("_Sorts")))
                {
                    dt.DefaultView.Sort = control.XmlAttributes["_Sorts"];
                    dt = dt.DefaultView.ToTable();
                }
                if (control.XmlAttributes.ContainsKey("_LocalDataSetTableName"))
                {
                    DataTable tmpDatatable = (mainBs.DataSource as DataSet).Tables[control.XmlAttributes["_LocalDataSetTableName"]];
                    dt.TableName = tmpDatatable.TableName;
                    (mainBs.DataSource as DataSet).Tables.Remove(tmpDatatable);
                }
                else
                {
                    // add _LocalDataSetTableName to properties
                    control.SetPropertyValue("_LocalDataSetTableName", dt.TableName);
                }
                (mainBs.DataSource as DataSet).Tables.Add(dt);


                //if(withRelation)
                //{
                //mainDataSet.Relations.Add(mainTableName + "_" + dt.TableName,
                //                dt.Columns[cntrl.XmlAttributes["_KeyField"]],
                //                mainDataSet.Tables[mainTableName].Columns[cntrl.XmlAttributes["XMLAttribute"]], true);
                //}
                bs.DataSource = (mainBs.DataSource as DataSet);
                bs.DataMember = dt.TableName;
                //setup binding

                control.Binding = bs;
                control.SetValue(mainBs);

            }
        }
    }
}
